﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using Hope.Gallery.Core;
using Hope.Gallery.Models;

namespace Hope.Gallery.ViewModels
{
    public class AdminViewModel : BaseViewModel
    {
        
        public AdminViewModel()
        {
           
        }

        #region Album

        public void AddAlbum(Album album)
        {
            bl.AddAlbum(album);
            bl.SaveChanges();
        }

        public void AddPictureInAlbum(int albumId, string fileName)
        {
            bl.AddAlbumDetail(albumId, fileName);
            bl.SaveChanges();
        }

        #endregion

      

        #region Upload photo

        /// <summary>
        /// Upload logic, this is called by the upload component
        /// </summary>
        /// <param name="chunk"></param>
        /// <param name="name"></param>
        /// <param name="album"></param>
        /// <returns></returns>
        public string Upload(int? chunk, string name, string album)
        {
            var fileUpload = HttpContext.Current.Request.Files[0];
            var uploadPath = DirectoryHelper.GetPhysicalUploadPathForAlbum(album);

            chunk = chunk ?? 0;
            using (var fs = new FileStream(Path.Combine(uploadPath, name), chunk == 0 ? FileMode.Create : FileMode.Append))
            {
                var buffer = new byte[fileUpload.InputStream.Length];
                fileUpload.InputStream.Read(buffer, 0, buffer.Length);
                fs.Write(buffer, 0, buffer.Length);
            }

            //return Content("chunk uploaded", "text/plain");
            return "chunk uploaded";
        }


        #endregion

        //#region Thumbnail
        ///// <summary>
        ///// Create the thumbnail for the uploaded picture
        ///// </summary>
        ///// <param name="uploadedFile"></param>
        ///// <param name="album"></param>
        //public void CreateThumbnail(string uploadedFile, string album)
        //{
        //    string uploadedFilePath = DirectoryHelper.GetUploadedFilePath(uploadedFile, album);
        //    string thumbPath = DirectoryHelper.GetThumbPathForAlbum(album, uploadedFile);

        //    PictureResize.ResizePicture(uploadedFilePath, thumbPath);
        //}


        //#endregion
    }
}